package com.telecom.controller.admin;

import com.telecom.common.convention.result.PageDTO;
import com.telecom.common.convention.result.PageQuery;
import com.telecom.common.convention.result.Result;
import com.telecom.pojo.dto.admin.order.AdminOrderPageQueryRequestDTO;
import com.telecom.pojo.dto.admin.order.OrderCancelRequestDTO;
import com.telecom.pojo.vo.admin.order.AdminOrderDetailVO;
import com.telecom.pojo.vo.admin.order.AdminOrderItemVO;
import com.telecom.pojo.vo.admin.order.RefundRecordVO;
import com.telecom.service.OrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;

/**
 * 管理端订单管理接口
 */
@RestController
@RequestMapping("/admin/order")
@Slf4j
public class AdminOrderController {

    @Resource
    OrderService orderService;

    /**
     * 订单分页查询
     * 
     * @param queryRequest 查询参数
     * @return 订单列表分页结果
     */
    @GetMapping("/page")
    public Result<PageDTO<AdminOrderItemVO>> page(@Valid AdminOrderPageQueryRequestDTO queryRequest) {
        log.info("管理端订单分页查询，查询参数：{}", queryRequest);
        PageDTO<AdminOrderItemVO> pageResult = orderService.pageQuery(queryRequest);
        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     * 
     * @param id 订单id
     * @return 订单详情
     */
    @GetMapping("/details/{id}")
    public Result<AdminOrderDetailVO> getOrderDetails(@PathVariable @Positive(message = "订单ID必须为正数") Long id) {
        log.info("查询订单详情，订单ID：{}", id);
        AdminOrderDetailVO orderDetails = orderService.getOrderDetails(id);
        return Result.success(orderDetails);
    }

    /**
     * 按订单号确认取餐
     * 
     * @param orderNumber 订单号
     * @return 确认结果
     */
    @PutMapping("/get/{orderNumber}")
    public Result<Void> confirmPickup(@PathVariable String orderNumber) {
        log.info("确认取餐，订单号：{}", orderNumber);
        orderService.confirmPickup(orderNumber);
        return Result.success();
    }

    /**
     * 取消订单
     * 
     * @param cancelRequest 取消订单请求参数
     * @return 取消结果
     */
    @PutMapping("/cancel")
    public Result<Void> cancelOrder(@RequestBody @Valid OrderCancelRequestDTO cancelRequest) {
        log.info("取消订单，请求参数：{}", cancelRequest);
        orderService.cancelOrder(cancelRequest);
        return Result.success();
    }

    /**
     * 退款记录分页查询
     *
     * @param page 分页参数
     * @return 退款记录列表分页结果
     */
    @GetMapping("/refund/page")
    public Result<PageDTO<RefundRecordVO>> getRefundPage(@Valid PageQuery page) {
        log.info("退款记录分页查询，分页参数：{}", page);
        PageDTO<RefundRecordVO> pageResult = orderService.getRefundPage(page);
        return Result.success(pageResult);
    }
}